﻿using DDD.Application;
using DDD.Core;
using DotNetCore.CAP;
using MediatR;
using Microsoft.Extensions.Logging;
using OrderModule.Domain.OrdeDomainEvent;
using OrderModule.Domain.OrderAggregate;
using OrderModule.IntegrationEvents;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OrderModule.Application.DomianEventsHandler
{
    internal class OrderCreatedDomainEventHandler(
        ICapPublisher publisher,
        ILogger<OrderCreatedDomainEventHandler> logger)
        : IDomainEventHandler<OrderCreatedDomainEvent>
    {
        public async Task Handle(OrderCreatedDomainEvent @event, CancellationToken cancellationToken)
        {
            var dict = @event.Order.Items.ToDictionary(e => e.GoodsId, e => e.Quantity);
            await publisher.PublishAsync(nameof(OrderCreatedIntegrationEvent), new OrderCreatedIntegrationEvent(@event.Order.Id, dict));
        }
    }
}
